package com.learning.dynamic.executor.configcenter.adapter;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.learning.dynamic.executor.configcenter.AbstractDynamicConfigCenter;
import com.learning.dynamic.executor.configcenter.DynamicConfigCenter;
import com.learning.dynamic.executor.enums.DynamicConfigCenterEnum;
import org.springframework.stereotype.Component;

/**
 * @author Wang Xu
 * @date 2023/11/7
 */
public class NacosConfigCenterAdapter extends AbstractDynamicConfigCenter<ConfigService> {

    @NacosInjected
    public NacosConfigCenterAdapter(ConfigService configService) {
        super(configService);
    }

    @Override
    public String getType() {
        return DynamicConfigCenterEnum.NACOS.getType();
    }

    @Override
    public String getConfigValue(String key) {
        return getConfigValue(key, 100);
    }

    @Override
    public String getConfigValue(String key, long timeoutMs) {
        try {
            return super.getConfigService().getConfig(key, "group-test1", timeoutMs);
        } catch (NacosException e) {
            LOGGER.error("query nacos config failed, key: {}, error msg is: ", key, e);
        }
        return null;
    }
}